(function() {
    'use strict';

    /**
         * @ngdoc directive
         * @name webApp.directive:layoutlist
         * @description
         * # layoutlist 

        用法：
        <layoutlist ng-model="chosedMember" set-member="setMember(member)"></layoutlist>
        setMember格式：function(member){}
    
        */

    angular.module('webApp')
        //调用其他方法
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.layoutlist.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .directive('layoutList', ['dataService', '$compile', 'moment', function(dataService, $compile, moment) {
            return {
                restrict: 'E',
                // require: 'ngModel',
                replace: true,
                scope: {
                    // storeid: '@', //店铺id
                    wxappid: '@', //小程序id
                    type: '@', //页面
                    callback: '&', //回调方法
                },
                template: "<span><a href='javascript:void(0);' ng-click='chose();' class='sui-btn btn-xlarge btn-success' style='padding:2px 20px;font-size:9px;border-radius:0px;'>{{'恢复'|translate}}</span>",
                controller: ['$templateCache', '$rootScope', '$scope', '$timeout', 'helper', function($templateCache, $rootScope, $scope, $timeout, helper) {
                    $scope.items = [];
                    $scope.chose = function() {
                        if ($scope.wxappid) {
                            $scope.wxapp_id = $scope.wxappid;
                        }
                        $scope.pageData(1).then(showPop);
                    };

                    $scope.wxaacallback = function(id) {
                        console.error("id====", id);
                        if (id) {
                            $scope.wxapp_id = id;
                            $scope.pageData();
                        }
                    }

                    //保存使用
                    $scope.save = function(l_id) {
                        if (l_id) {
                            _.map($scope.userlayouts, function(value, key) {
                                if (value.user_layout_id == l_id) {
                                    $scope.saveData = value;
                                }
                            });
                            rmAlert();
                            return helper.myAlert("已恢复，保存后生效。", null, null, function() {
                                $timeout(function() {
                                    if ($scope.callback) {
                                        $scope.callback({
                                            conten: $scope.saveData.content
                                        });
                                    }
                                });
                            });
                        }
                    };

                    //删除
                    $scope.delete = function(l_id) {
                        // userlayoutDel
                        dataService.userlayoutDel(l_id).then(function(list) {
                            if (!list.Msg) {
                                rmAlert();
                                return helper.myAlert("success");
                            }
                        });
                    };

                    function showPop() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '<form class="sui-form" style="margin:0px;"><span>{{"备份列表" | translate}}</span></form>',
                            body: $templateCache.get("views/directives.layoutlist.html"),
                            // okBtn: '{{"ok" | translate}}',
                            hasfoot: false,
                            closeBtn: true,
                            transition: true,
                            width: 'large',
                            height: '400',
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {

                            },
                        });
                    }

                    //请求数据
                    $scope.pageData = function(pageNo) {
                        var saveData = {
                            content: {
                                type: $scope.type,
                                // store_id: $scope.storeid,
                                wxapp_id: $scope.wxapp_id,
                                page_no: pageNo,
                                page_size: $scope.page_Size ? $scope.page_Size : 10,
                            }
                        };

                        return dataService.userlayoutGet(saveData).then(function(list) {
                            if (list) {
                                $scope.userlayouts = list.userlayouts;
                                $scope.userlayoutsTotal = list.total_count;
                            }
                        });
                    }
                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        }])
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.udpanel.tp1.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.udpanel.tp2.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.udpanel.tp3.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.udpanel.tp4.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.udpanel.tp5.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.udpanel.tp6.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.udpanel.tp7.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .directive('udpanel', ['dataService', '$compile', 'moment', function(dataService, $compile, moment) {
            return {
                restrict: 'E',
                require: 'ngModel',
                replace: true,
                scope: {
                    ngModel: '=', //组件数据
                },
                templateUrl: "views/directives.udpanel.html",
                controller: ['$templateCache', '$rootScope', '$scope', '$timeout', 'helper', function($templateCache, $rootScope, $scope, $timeout, helper) {
                    $scope.tp = {
                        "1": $templateCache.get("views/directives.udpanel.tp1.html"),
                        "2": $templateCache.get("views/directives.udpanel.tp2.html"),
                        "3": $templateCache.get("views/directives.udpanel.tp3.html"),
                        "4": $templateCache.get("views/directives.udpanel.tp4.html"),
                        "5": $templateCache.get("views/directives.udpanel.tp5.html"),
                        "6": $templateCache.get("views/directives.udpanel.tp6.html"),
                        "7": $templateCache.get("views/directives.udpanel.tp7.html"),
                    };
                    $scope.ngModel.value = $scope.ngModel.value || [];


                    $scope.Selectdata = function(type) {
                        if (type) {
                            $scope.ngModel.value = $scope.ngModel.value.length ? $scope.ngModel.value : '';
                            $scope.ngModel.value += $scope.tp[type];
                        }
                    }

                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        }])
        .directive('bianJu', ['dataService', '$compile', 'moment', function(dataService, $compile, moment) {
            return {
                restrict: 'E',
                require: 'ngModel',
                replace: true,
                scope: {
                    ngModel: '=', //组件数据
                },
                templateUrl: "views/directives.bianju.html",
                controller: ['$templateCache', '$rootScope', '$scope', '$timeout', 'helper', function($templateCache, $rootScope, $scope, $timeout, helper) {



                    $scope.init = function() {
                        // console.error("$scope.ngModel", $scope.ngModel);
                        $scope.newmoder = []
                        _.map($scope.ngModel, function(da, ke) {
                            if (da.name == 'marginTop' || da.name == 'marginBottom' || da.name == 'marginLeft' || da.name == 'marginRight' || da.name == 'paddingTop' || da.name == 'paddingBottom' || da.name == 'paddingLeft' || da.name == 'paddingRight') {
                                $scope.newmoder[da.name] = da;
                            }
                        });
                    }

                    //加减 
                    $scope.clickdata = function(key, val) {
                        if (val == 'jian') {
                            // if ($scope.newmoder[key].value > 0) {
                            //     $scope.newmoder[key].value = $scope.newmoder[key].value - 1;
                            // }
                            $scope.newmoder[key].value = $scope.newmoder[key].value - 1;
                        } else {

                            $scope.newmoder[key].value = $scope.newmoder[key].value + 1;
                        }
                        _.map($scope.ngModel, function(data, ke) {
                            if (data.name == key) {
                                data.value = $scope.newmoder[key].value;
                            }
                        });
                    }

                    $scope.Selectdata = function(type) {
                        if (type) {
                            $scope.ngModel.value = $scope.ngModel.value.length ? $scope.ngModel.value : '';
                            $scope.ngModel.value += $scope.tp[type];
                        }
                    }

                    $scope.init();
                    $scope.$on('event:up-store-id', function() {
                        $scope.init();
                    });
                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        }])
        .directive('baomingForm', ['dataService', '$compile', 'moment', function(dataService, $compile, moment) {
            return {
                restrict: 'E',
                require: 'ngModel',
                replace: true,
                scope: {
                    ngModel: '=', //组件数据
                },
                templateUrl: "views/directives.baomingform.html",
                controller: ['$templateCache', '$rootScope', '$scope', '$timeout', 'helper', function($templateCache, $rootScope, $scope, $timeout, helper) {

                    $scope.init = function() {
                        $scope.newmoder = []
                        _.map($scope.ngModel, function(da, ke) {
                            if (da.name == 'validInput' || da.name == 'inputType') {
                                var das = clone(da);
                                var isdata = {};
                                _.map(das.list_values, function(data, ke) {
                                    if (data == 'nick_name') {
                                        isdata[data] = "姓名";
                                    }
                                    if (data == 'mobile') {
                                        isdata[data] = "手机号";
                                    }
                                    if (data == 'avatar') {
                                        isdata[data] = "头像";
                                    }
                                    if (data == 'weixin') {
                                        isdata[data] = "微信";
                                    }
                                    if (data == 'qq') {
                                        isdata[data] = "qq";
                                    }
                                    if (data == 'province' || data == 'provice') {
                                        isdata[data] = "省份";
                                    }
                                    if (data == 'city') {
                                        isdata[data] = "城市";
                                    }
                                    if (data == 'district') {
                                        isdata[data] = "地区";
                                    }
                                    if (data == 'address') {
                                        isdata[data] = "地址";
                                    }
                                    if (data == 'name') {
                                        isdata[data] = "姓名";
                                    }
                                    if (data == 'phone') {
                                        isdata[data] = "电话";
                                    }
                                    if (data == 'content') {
                                        isdata[data] = "详细内容";
                                    }
                                    if (data == 'email') {
                                        isdata[data] = "邮箱";
                                    }
                                    if (data == 'identify_id') {
                                        isdata[data] = "身份证号";
                                    }
                                    if (data == 'identify_img_front') {
                                        isdata[data] = "身份证正面照";
                                    }
                                    if (data == 'identify_img_back') {
                                        isdata[data] = "身份证背面照";
                                    }
                                });
                                das.list_values = isdata;
                                $scope.newmoder[da.name] = das;
                            }

                        });

                    }

                    $scope.check = function(type, checked) {
                        if (type) {
                            if (type == "inputType") {
                                // console.error("type",type);
                                var aa = $scope.newmoder[type].value;
                                $scope.newmoder['validInput'].value = clone(aa);
                                _.map($scope.ngModel, function(da, ke) {
                                    if (da.name) {
                                        da.value = $scope.newmoder[type].value;
                                    }
                                });
                            } else {
                                _.map($scope.ngModel, function(da, ke) {
                                    if (da.name == type) {
                                        da.value = $scope.newmoder[type].value;
                                    }
                                });
                            }

                            //console.error($scope.ngModel,$scope.newmoder);
                        }
                    }

                    $scope.init();
                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        }])
        .directive('uimodList', ['dataService', '$compile', 'moment', function(dataService, $compile, moment) {
            return {
                restrict: 'E',
                require: 'ngModel',
                replace: true,
                scope: {
                    bodyHeight: '@',
                    ngModel: '=', //组件数据
                    callback: '&', //回调方法
                },
                templateUrl: "views/directives.uimodlist.html",
                controller: ['$templateCache', '$rootScope', '$scope', '$timeout', 'helper', '$window', function($templateCache, $rootScope, $scope, $timeout, helper, $window) {
                    $scope.init = function() {
                        $scope.newData = {};
                        $scope.photoshow = {
                            "search": 'search-minus',
                            "catlist": 'map-o',
                            "catitems_list": 'list-ul',
                            "slider": 'window-maximize',
                            "order_additional_form": 'window-restore',
                            "storelog": 'map-signs',
                            "topbar": 'paper-plane-o',
                            "tuangou": 'shopping-bag',
                            "udcontent": 'pencil-square-o',
                            "udicons": 'magic',
                            "uditem_1": 'photo',
                            "uditem_2": 'clone',
                            "uditem_3": 'navicon',
                            "udpanel": 'object-group',
                            "udprompt": 'volume-up',
                            "userfx": 'handshake-o',
                            "detail_slider": 'window-maximize',
                            "catitems_udcid": 'file-image-o',
                            "catitems": 'th-large',
                            "liuyan_form": 'sticky-note-o',
                            "facepayment": 'rmb',
                            "udnotice": 'sticky-note-o',
                            "rollcontent": 'sticky-note-o',
                            "bottomCopyright": 'copyright',
                            "pagebackground": 'fa-paper-plane-o',
                            "kanjiaui": 'cog',
                            "detail_price": 'rmb',
                            "detail_pingjia": 'list-alt',
                            "detail_kefufav": 'star-o',
                            "detail_postfee": 'navicon',
                        };
                        $scope.tubiao = {
                            "search": 'search.png',
                            "catlist": 'cat.png',
                            "catitems_list": 'catitem.png',
                            "slider": 'slider.png',
                            "order_additional_form": 'diaocha.png',
                            "storelog": 'storelog.png',
                            "topbar": 'topbar.png',
                            "tuangou": 'miaosha.png',
                            "udcontent": 'udpanel.png',
                            "udicons": 'udicons.png',
                            "uditem_1": 'item1.png',
                            "uditem_2": 'item2.png',
                            "uditem_3": 'item3.png',
                            "udpanel": 'udpanel.png',
                            "udprompt": 'modal.png',
                            "userfx": 'userfx.png',
                            "detail_slider": 'slider.png',
                            "catitems_udcid": "catidItem.png",
                            "catitems": 'cat.png',
                            "liuyan_form": 'diaocha.png',
                            "facepayment": 'rmb',
                            "udnotice": 'notice.png',
                            "rollcontent": 'wenzhang.png',
                            "bottomCopyright": 'banquan.png',
                            "wenzhanglist": "wenzhang.png",
                            "diaochawenjuan": "diaocha.png",
                            "uditem_4": "item2.png",
                            "uditem_5": "item2.png",
                            "uditem_6": "item6.png",
                            "choiceStore": "choicestore.png",
                            "storelist": "storelist.png",
                            "weblink": "webhtml.png",
                            "pintuanlist": "tuan.png",
                            "coupons": "coupon.png",
                            "catrent_udcid": "jiudian.png",
                            "huodonglist": "huodonglist.png",
                            "guanggao": "ad.png",
                            "openxcx": "openxcx.png",
                            "requ": "pin.png",
                            "udtitle": "udtitle.png",
                            "storeloginfo": "storeloginfo.png",
                            "pinitem": "pinitem.png",
                            "pinitemscrollx": "pinitem.png",
                            "itemtuijian": "itemtuijian.png",
                            "couponDialog": "coupondialog.png",
                            "commentary": "commentary.png",
                            "detail_postfee": "detail_postfee.png",
                            "detail_kefufav": "detail_kefufav.png",
                            "detail_pingjia": "detail_pingjia.png",
                            "detail_price": "detail_price.png",
                            "pagebackground": "topbar.png",
                            "kanjiaui": "setting.png"
                        }
                        _.map($scope.ngModel, function(data, key) {
                            if (data.dft == '1') {
                                if (!$scope.newData) {
                                    $scope.newData[data.subtype] = [];
                                    $scope.newData[data.subtype].push(data);
                                } else {
                                    if ($scope.newData[data.subtype]) {
                                        $scope.newData[data.subtype].push(data);
                                    } else {
                                        $scope.newData[data.subtype] = [];
                                        $scope.newData[data.subtype].push(data);
                                    }
                                }
                            }
                        });
                        // console.error("newData", $scope.newData);
                        _.map($scope.newData, function(isdata, key) {
                            isdata.len = 0;
                            isdata.num = 0;
                            _.map(isdata, function(da, k) {
                                if (da.select) {
                                    isdata.num++;
                                }
                                isdata.len++;
                            });
                        });
                    }
                    $('#layoutMenu').height($window.innerHeight - 181 + "px")
                    $scope.tianjia = function(key, num) {
                            console.error("key", key, num);
                            if (!num) {
                                return helper.myAlert("该组件已添加！");
                            }
                            if (key) {
                                $scope.select_key = key;
                                for (var i = $scope.ngModel.length - 1; i >= 0; i--) {
                                    if ($scope.ngModel[i].subtype == key) {
                                        if (!$scope.ngModel[i].select) {
                                            $scope.ngModel[i].select = 1;
                                            if ($scope.callback) {
                                                // console.error("$scope.ngModel[i]", $scope.ngModel[i]);
                                                $scope.callback({
                                                    data: $scope.ngModel[i]
                                                });
                                            }
                                            break;
                                        }
                                    }
                                }
                                $scope.init();
                            }
                        }
                        // $scope.init();
                }],
                link: function(scope, element, attrs, ctrl) {

                    scope.$watch('ngModel', function(newValue, oldValue, scope) {
                        if (newValue != oldValue) {
                            // console.error("$scope.ngModel[i]",newValue);
                            scope.init();
                        }
                    }, true);
                }
            }
        }])
        .directive('layoutStoreOne', ['dataService', '$compile', 'moment', function(dataService, $compile, moment) {
            return {
                restrict: 'E',
                replace: true,
                scope: {
                    type: "@",
                    desc: "@",
                    notice: "@",
                },
                template: '<div style="display:flex;"> <div style="display:inline"><img ng-src="{{store.logo_img}}" style="border-radius:5px;width:67px;height: 57px;margin:3px;margin-left:24px;margin-right: 8px"></div><div style="display:inline-block;margin-left:15px;"><div style="font-size: 14px;">{{store.name}}</div> <div ng-show="type ==1">电话：{{store.phone}}</div>  <div ng-show="type ==1">地址：{{store.address}}</div> <div ng-show="type ==2" style="width: 200px;overflow: hidden; white-space: nowrap;text-overflow: ellipsis;">{{desc}}</div>  <div ng-show="type ==2" style="width: 200px;overflow: hidden; white-space: nowrap;text-overflow: ellipsis;"><div style="background-color: #F4C93E;color: #1A1A1A;width: 27px; ">公告</div>{{notice}}</div></div></div></div>',
                controller: ['$templateCache', '$rootScope', '$scope', '$timeout', 'helper', function($templateCache, $rootScope, $scope, $timeout, helper) {
                    //请求数据
                    $scope.getone = function(pageNo) {
                        dataService.storeOne($rootScope.store_id).then(function(retun) {
                            if (retun) {
                                $scope.store = retun;
                                //console.error("retun",retun);
                            }
                        });
                    }
                    $scope.getone();
                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        }])
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.adminlayoutlist.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .directive('upLayout', ['dataService', '$compile', 'moment', function(dataService, $compile, moment) {
            return {
                restrict: 'E',
                replace: true,
                scope: {
                    storeid: "@",
                    wxappid: '@', //小程序id
                },
                template: "<span><a href='javascript:void(0);' ng-click='chose();' class='sui-btn btn-success'>{{'修改装修模板'|translate}}</span>",
                controller: ['$templateCache', '$rootScope', '$scope', '$timeout', 'helper', function($templateCache, $rootScope, $scope, $timeout, helper) {
                    $scope.items = [];
                    $scope.chose = function() {
                        console.error("$scope.ngModel", $scope.storeid, $scope.wxappid);
                        $scope.pageData(1).then(showPop);
                    };
                    //保存使用
                    $scope.save = function(value) {
                        if (value) {
                            var saveData = {
                                store_id: $scope.storeid,
                                app_id: $scope.wxappid,
                                name: "UI-MOd-home",
                                appid: $scope.wxappid,
                                types: "ui"
                            };
                            $scope.wxappOne(saveData).then(function(type) {
                                var post = type ? "wxappUpdate" : "wxappAdd";
                                saveData.value = JSON.parse(value);
                                dataService[post](saveData).then(function(list) {
                                    if (list.app_id) {
                                        helper.myAlert("success");
                                    } else {
                                        helper.myAlert(list.Msg);
                                    }
                                });
                            })
                        }
                    };

                    $scope.wxappOne = function(saveData) {
                        return dataService.wxappOne(saveData).then(function(list) {
                            if (list) {
                                return list.app_id;
                            } else {
                                return false;
                            }
                        });
                    }

                    function showPop() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '<form class="sui-form" style="margin:0px;"><span>{{"备份列表" | translate}}</span></form>',
                            body: $templateCache.get("views/directives.adminlayoutlist.html"),
                            // okBtn: '{{"ok" | translate}}',
                            hasfoot: false,
                            closeBtn: true,
                            transition: true,
                            width: 'large',
                            height: '400',
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {

                            },
                        });
                    }

                    //请求数据
                    $scope.pageData = function(pageNo) {
                        var query = {
                            content: {
                                page_no: pageNo,
                                page_size: $scope.pageSize ? $scope.pageSize : 10,
                            }
                        };
                        return dataService.adminlayoutGet(query).then(function(list) {
                            if (list) {
                                $scope.adminlayouts = list.adminlayouts;
                                $scope.total_count = list.total_count;
                                return $scope.adminlayouts;
                            }
                        });
                    }
                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        }])
})();